(function($) {
	var myflow = $.myflow;

	var util = {
		getFormType : function(src) {
			var form;
			if (src.getFormType) {
				form = src.getFormType();
			}
			if (window.console) {
				window.console.log(form);
			}
			return form;
		},
		initPopEvent : function(sle, panel, that) {
			sle.click(function() {
				if (window["shownPanel"]) {
					window["shownPanel"].trigger("hiddenPanel");
				}
				panel.show();
				window["shownPanel"] = panel;
			});
			panel.bind("hiddenPanel", function() {
				window["shownPanel"] = null;
				panel.hide();
				try {
					that.destroy();
				} catch (e) {
					if (console) {
						console.log(e);
					}
				}
			});
			$(".pop-editor-panel select").live("click", function() {
				panel.addClass("out-by-select");
			});

			panel.mousemove(function() {
				window["needHiddenPanel"] = null;
			});
			panel.hover(function() {
				window["needHiddenPanel"] = null;
				panel.removeClass("out-by-select");
			}, function() {
				if (panel.hasClass("out-by-select")) {
					window["needHiddenPanel"] = panel;
				} else {
					// window["needHiddenPanel"].trigger("hiddenPanel");
				}
			});
			// $(document).click(function() {
			// if (window["needHiddenPanel"]) {
			// window["needHiddenPanel"].trigger("hiddenPanel");
			// }
			// });

			$(".pop-panel-close-btn", panel).click(function() {
				panel.trigger("hiddenPanel");
			});

		},
		fillSelectOptionNotice : function($obj) {
			$obj.empty();
			$obj.append('<option value="">加载中...</option>');
		},
		fillSelectOption : function($obj, data, cv, name, value, nullOption) {
			if (!name) {
				name = "name";
			}
			if (!value) {
				value = "value";
			}
			$obj.empty();
			if (nullOption) {
				$obj.append('<option value="">请选择</option>');
			}
			if (data && data.length) {
				var l = data.length;
				for (var i = 0; i < l; i++) {
					$obj.append('<option value="' + data[i][value] + '">' + data[i][name] + '</option>');
				}
				$obj.val(cv);
			}
		},
		loadAbleOwner : function(form, type, callback) {
			var _key = form + "_" + type;
			var data = util.data[_key];
			var name, value;
			// if (type == "role") {
			name = "role_description";
			value = "role_code";
			// } else {
			// value = "relate_role_code";
			// name = "relate_role_name";
			// }
			if (!data || data.length == 0) {
				data = [];
				myUtil.loadDataSources(myflow.config.contextPath + "/frame/definition/getOwner.do?object_type=" + form + "&type=" + type, function(data) {
					callback(data, name, value);
					util.data[_key] = data;
				}, "owner_" + form + "_" + type);
			} else {
				callback(data, name, value);
			}
		},
		loadAbleTask : function(form, callback) {
			var data = util.data.task;
			if (!data || data.length == 0) {
				data = [];
				myUtil.loadDataSources(myflow.config.contextPath + "/frame/definition/getTaskDefByObjTypeAndWfDef.do?_id="+flowId+"&object_type=" + form, function(data) {
					callback(data, "_id", "name");
					myUtil.data.task = data;
				}, "task_" + form);
			} else {
				callback(data, "_id", "name");
			}
		},
		loadAbleWorkflow : function(form, callback) {
			var data = util.data.workflow;
			if (!data || data.length == 0) {
				data = [];
				myUtil.loadDataSources(myflow.config.contextPath + "/frame/definition/getWFByDefIdAndEnableWf.do?_id="+flowId+"&object_type=" + form, function(data) {
					callback(data, "defname", "description");
					myUtil.data.workflow = data;
				}, "workflow_" + form);
			} else {
				callback(data, "_id", "name");
			}
		},
		loadFormField : function(form, callback, base) {
			var cacheKey = "form_" + form;
			if (base) {
				cacheKey = cacheKey + "_showBaseProp";
			}
			var data = util.data.formField[cacheKey];
			if (!data || data.length == 0) {
				data = [];
				var url = myflow.config.contextPath + "/frame/definition/getObjProperty.do?object_type=" + form;
				if (base) {
					url = url + "&showBaseProp=true";
				}
				myUtil.loadDataSources(url, function(data) {
					callback(data, "property_name", "description");
					util.data.formField[cacheKey] = data;
				}, cacheKey);
			} else {
				callback(data, "property_name", "description");
			}
		},
		buildTreeFormField : function(fields, text, value, callback, goonCheck) {
			for ( var key in fields) {
				if (key == 'node_data') {
					continue;
				}
				var data = fields[key];
				if (data['node_data']) {
					var item = {
						"read" : false,
						"write" : false,
						"default" : true
					};
					item[text] = data['node_data'][text];
					item[value] = data['node_data'][value];
					callback(item);
				}
				if (!goonCheck || goonCheck(data)) {
					util.buildTreeFormField(data, text, value, callback, goonCheck);
				}
			}
		},
		returnTreeFormField : function(json, text, value) {
			var data = [];
			util.buildTreeFormField(json, text, value, function(item) {
				data.push(item);
			}, function(data) {
				if (!data || !data["node_data"]) {
					return true;
				}
				return data["node_data"]['type'] != 'java.util.List';
			});
			return data;
		},
		data : {
			formField : {}
		}
	};

	$.extend(true, myflow.editors, {
		inputEditor : function(readonly) {
			var _props, _k, _div, _src, _r;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var input = $('<input style="width:100%;height:100%;"/>').val(props[_k].value).change(function() {
					props[_k].value = $(this).val();
				}).appendTo('#' + _div);
				if (readonly) {
					input.attr("readonly", "readonly").addClass("readonly");
				}

				$('#' + _div).data('editor', this);
			}
			this.destroy = function() {
				$('#' + _div + ' input').each(function() {
					_props[_k].value = $(this).val();
				});
			}
		},
		templateEditor : function(url, projtype) {
			var _props, _k, _div, _src, _r;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;
				var that = this;
				var sle = $('<select class="select_select"/>').val(props[_k].value).change(function() {
					that.destroy();
				}).click(function(){
					if (!projtype) {
						projtype = util.getFormType(src);
					}
					that.loadTemplate(sle,url,projtype);
				}).appendTo('#' + _div);
				sle.click();
				$('#' + _div).data('editor', this);
			};
			this.loadTemplate=function(sle,url,projtype){
				var cacheKey = "object_type_" + projtype;
				var _projtype=sle.data("projtype");
				if(projtype && projtype!=_projtype){
					var _url = url + "?object_type=" + projtype;
					util.fillSelectOptionNotice(sle);
					myUtil.loadDataSources(_url, function(data) {
						util.fillSelectOption(sle, data, _props[_k].value, "description", "_id");
						_props[_k].value = sle.val();
					}, cacheKey);
					sle.data("projtype",projtype);
				}
			};
			this.destroy = function() {
				$('#' + _div + ' select').each(function() {
					_props[_k].value = $(this).val() || '';
				});
			};
		},
		selectEditor : function(arg, cacheKey, name, value, changeEvent, initEvent) {
			var _props, _k, _div, _src, _r;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<select class="select_select"/>').val(props[_k].value).change(function() {
					that.destroy();
					if (changeEvent) {
						$(r).trigger(changeEvent, [ $(this).val(), src, _k ]);
					}

				}).appendTo('#' + _div);

				util.fillSelectOptionNotice(sle);
				if (typeof arg === 'string') {
					myUtil.loadDataSources(arg, function(data) {
						util.fillSelectOption(sle, data, _props[_k].value, name, value);
						_props[_k].value = sle.val();
						if (initEvent) {
							$(r).trigger(initEvent, [ sle.val(), src, _k ]);
						}
					}, cacheKey);
				} else {
					util.fillSelectOption(sle, arg, _props[_k].value, name, value);
					_props[_k].value = sle.val();
					if (initEvent) {
						$(r).trigger(initEvent, [ sle.val(), src, _k ]);
					}
				}
				$('#' + _div).data('editor', this);
			};
			this.destroy = function() {
				$('#' + _div + ' select').each(function() {
					_props[_k].value = $(this).val() || '';
				});
			};
		},
		rightEditor : function() {
			var _props, _k, _div, _src, _r;
			var panel;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<div class="pop-editor-label" style="width:100%;height:100%;"/>').text("...").attr("title", "点击编辑").appendTo('#' + _div);
				panel = $('<div class="pop-editor-panel"/>').appendTo('#' + _div);
				var html = [];
				html.push('<tr>');
				html.push('	<td style="height:30px;">');
				html.push('		<input type="hidden" class="input-control property_name"  name="property_name" readonly="readonly" style="height:30px;min-height:30px;"/>');
				html.push('		<input type="text" class="input-control description"  name="description" readonly="readonly" style="height:30px;min-height:30px;"/>');
				html.push('	</td>');
				html.push('	<th  class="width40">');
				html.push('		<label class="check-box-item" >');
				html.push('			<input type="checkbox" class="read" name="read"/>读');
				html.push('		</label>');
				html.push('	</th>');
				html.push('	<th  class="width40">');
				html.push('		<label class="check-box-item">');
				html.push('			<input type="checkbox" class="write" name="write" />写');
				html.push('		</label>');
				html.push('	</th>');
				html.push('	<th  class="width50">');
				html.push('		<label class="check-box-item">');
				html.push('			<input type="checkbox" class="default"  name="default"  />默认');
				html.push('		</label>');
				html.push('	</th>');
				html.push('</tr>');

				var tableHtml = [];
				tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
				tableHtml.push('<thead>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="4" class="pop-panel-header">');
				tableHtml.push('步骤授权配置');
				tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<tr>');
				tableHtml.push('<th>');
				tableHtml.push('属性');
				tableHtml.push('</th>');
				tableHtml.push('<th colspan="4">');
				tableHtml.push('属性权限');
				tableHtml.push('</th>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('</thead>');
				tableHtml.push('<tbody></tbody>');
				tableHtml.push('</Table>');

				var $table = $(tableHtml.join('')).appendTo(panel);

				var _valueJson;
				if (_props[_k].value) {
					_valueJson = myUtil.stringToJson(_props[_k].value);
				}
				util.loadFormField(util.getFormType(src), function(json, value, name) {
					util.buildTreeFormField(json, name, value, function(item) {
						if (_valueJson) {
							$.extend(true, item, _valueJson[item[value]]);
						}
						that.addRow($table, html, item);
					}, function(data) {
						if (!data || !data["node_data"]) {
							return true;
						}
						return data["node_data"]['type'] != 'java.util.List';
					});
				});
				$('#' + _div).data('editor', this);
				util.initPopEvent(sle, panel, that);

			};
			this.addRow = function($table, html, data) {
				var $tr = $(html.join('')).appendTo($table);
				if (data) {
					$tr.find(".property_name").val(data["property_name"]);
					$tr.find(".description").val(data["description"]);
					$tr.attr("title", data["description"]);
					if (data["read"]) {
						$tr.find(".read").attr("checked", "checked");
					}
					if (data["write"]) {
						$tr.find(".write").attr("checked", "checked");
					}
					if (data["default"]) {
						$tr.find(".default").attr("checked", "checked");
					}
					$tr.find(".read").change(function() {
						if ($(this).attr("checked") == "checked") {
							$tr.find(".default").removeAttr("checked");
						}
					});
					$tr.find(".write").change(function() {
						if ($(this).attr("checked") == "checked") {
							$tr.find(".read").attr("checked", "checked");
							$tr.find(".default").removeAttr("checked");
						}
					});
					$tr.find(".default").change(function() {
						if ($(this).attr("checked") == "checked") {
							$tr.find(".read").removeAttr("checked");
							$tr.find(".write").removeAttr("checked");
						}
					});
				}
			};
			this.destroy = function() {
				var array = {};
				panel.find("tbody tr").each(function() {
					var item = {};
					var _fn = $(this).find(".property_name").val();
					// item["property_name"] = _fn;
					item["read"] = $(this).find(".read").attr("checked") == "checked";
					item["write"] = $(this).find(".write").attr("checked") == "checked";
					item["default"] = $(this).find(".default").attr("checked") == "checked";
					array[_fn] = item;
				});
				_props[_k].value = array;
			};
		},
		ownersEditor : function() {
			var _props, _k, _div, _src, _r;
			var panel;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<div class="pop-editor-label" style="width:100%;height:100%;"/>').text("...").attr("title", "点击编辑").appendTo('#' + _div);
				panel = $('<div class="pop-editor-panel"/>').appendTo('#' + _div);
				var html = [];
				html.push('<tr>');
				html.push('	<th  align="center" valign="middle" class="panel-row-btn">');
				html.push('	- ');
				html.push('	</th>');
				html.push('	<td class="width60">');
				html.push('		<select class="input-control permissionType"  name="permissionType" >');
				html.push('			<option value="roleTag">角色标签</option>');
				html.push('			<option value="role">角色</option>');
				// html.push(' <option value="ind">个人</option>');
				html.push('			<option value="superior">直属上级</option>');
				html.push('			<option value="custom">自定义</option>');
				html.push('		</select>');
				html.push('	</td>');
				html.push('	<th  align="center" valign="middle" class="panel-row-rel">');
				html.push('		=');
				html.push('	</th>');
				html.push('	<td>');
				html.push('	  <select class="input-control permissionObject"  name="permissionObject">');
				html.push('	  </select>');
				html.push('	</td>');
				html.push('</tr>');

				var tableHtml = [];
				tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
				tableHtml.push('<thead>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="4" class="pop-panel-header">');
				tableHtml.push('分配角色配置');
				tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<tr>');
				tableHtml.push('<th>');
				tableHtml.push('&nbsp;');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('类型');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('条件');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('对象');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('</thead>');
				tableHtml.push('<tbody></tbody>');
				tableHtml.push('</Table>');

				var $table = $(tableHtml.join('')).appendTo(panel);

				var $add = $('<div class="panel-add-btn ui-widget-header">+</div>').appendTo(panel);
				$add.click(function() {
					that.addRow($table, html, {}, util.getFormType(src));
				});
				var json = myUtil.stringToJson(_props[_k].value);
				for ( var i in json) {
					that.addRow($table, html, json[i], util.getFormType(src));
				}
				$('#' + _div).data('editor', this);
				util.initPopEvent(sle, panel, that);
			};
			this.addRow = function($table, html, data, form) {
				var $tr = $(html.join('')).appendTo($table);
				$tr.find(".panel-row-btn").click(function() {
					$tr.remove();
				});
				var _dv;
				if (data) {
					for ( var k in data) {
						$tr.find("." + k).val(data[k]);
					}
					_dv = data["permissionObject"];
				}
				var $type = $tr.find(".permissionType");
				var $value = $tr.find(".permissionObject");
				$type.change(function() {
					var val = this.value;
					var parent = $value.parent();
					$value.remove();
					if (val == 'custom') {
						$value = $('<input class="input-control permissionObject"  name="permissionObject"/>').appendTo(parent);
					} else {
						$value = $('<select class="input-control permissionObject"  name="permissionObject"/>').appendTo(parent);
					}
					if (val == data['permissionType']) {
						$value.val(data['permissionObject']);
					}
					if (val == 'superior') {
						$value.css("visibility", "hidden");
					} else {
						$value.css("visibility", "visible");
						util.fillSelectOptionNotice($value);
						util.loadAbleOwner(form, val, function(data, name, value) {
							util.fillSelectOption($value, data, _dv, name, value);
						});
					}
				}).change();
			};
			this.destroy = function() {
				var array = [];
				panel.find("tbody tr").each(function() {
					var item = {};
					$(this).find(".input-control").each(function() {
						item[$(this).attr("name")] = $(this).val();
					});
					array.push(item);
				});
				_props[_k].value = array;
			};
		},
		enterconditionsEditor : function() {
			var _props, _k, _div, _src, _r;
			var panel;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<div class="pop-editor-label" style="width:100%;height:100%;"/>').text("...").attr("title", "点击编辑").appendTo('#' + _div);
				panel = $('<div class="pop-editor-panel"/>').appendTo('#' + _div);

				var html = [];
				html.push('<tr>');
				html.push('	<th  align="center" valign="middle" class="panel-row-btn">');
				html.push('	- ');
				html.push('	</th>');
				html.push('	<td>');
				html.push('		<select class="input-control condition_name"  name="condition_name">');
				html.push('		</select>');
				html.push('	</td>');
				html.push('	<td class="width50">');
				html.push('		<select class="input-control condition_op"  name="condition_op">');
				html.push('			<option value="=">等于</option>');
				html.push('			<option value="!=">不等于</option>');
				html.push('			<option value=">">大于</option>');
				html.push('			<option value=">=">大于等于</option>');
				html.push('			<option value="<">小于</option>');
				html.push('			<option value="<=">小于等于</option>');
				html.push('			<option value="in">被包含</option>');
				html.push('			<option value="not_in">不被包含</option>');
				html.push('			<option value="include">包含</option>');
				html.push('			<option value="not_include">不包含</option>');
				html.push('		</select>');
				html.push('	</td>');
				html.push('	<td>');
				html.push('	  <input type="text" class="input-control condition_value"  name="condition_value"/>');
				html.push('	</td>');
				html.push('</tr>');

				var tableHtml = [];
				tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
				tableHtml.push('<thead>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="4" class="pop-panel-header">');
				tableHtml.push('进入条件配置');
				tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<tr>');
				tableHtml.push('<th>');
				tableHtml.push('&nbsp;');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('属性');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('条件类型');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('条件值');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('</thead>');
				tableHtml.push('<tbody></tbody>');
				tableHtml.push('</Table>');

				var $table = $(tableHtml.join('')).appendTo(panel);
				var $add = $('<div class="panel-add-btn ui-widget-header">+</div>').appendTo(panel);
				$add.click(function() {
					that.addRow($table, html, {}, util.getFormType(src));
				});
				var data = myUtil.stringToJson(_props[_k].value);
				for ( var i in data) {
					that.addRow($table, html, data[i], util.getFormType(src));
				}
				$('#' + _div).data('editor', this);
				util.initPopEvent(sle, panel, that);
			};
			this.addRow = function($table, html, data, form) {
				var $tr = $(html.join('')).appendTo($table);
				$tr.find(".panel-row-btn").click(function() {
					$tr.remove();
				});
				var _dv;
				if (data) {
					for ( var k in data) {
						$tr.find("." + k).val(data[k]);
					}
					_dv = data["condition_name"];
				}
				var $field = $tr.find(".condition_name");
				util.fillSelectOptionNotice($field);
				util.loadFormField(form, function(json, value, name) {
					util.fillSelectOption($field, util.returnTreeFormField(json, name, value), _dv, name, value);
				}, true);
			};
			this.destroy = function() {
				var array = [];
				panel.find("tbody tr").each(function() {
					var item = {};
					$(this).find(".input-control").each(function() {
						item[$(this).attr("name")] = $(this).val();
					});
					array.push(item);
				});
				_props[_k].value = array;
			};
		},
		taskEditor : function() {
			var _props, _k, _div, _src, _r;
			var panel, copyPanel;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<div class="pop-editor-label" style="width:100%;height:100%;"/>').text("...").attr("title", "点击编辑").appendTo('#' + _div);
				panel = $('<div class="pop-editor-panel"/>').appendTo('#' + _div);

				var html = [];
				html.push('<tr>');
				html.push('	<th  align="center" valign="middle" class="panel-row-btn">');
				html.push('	- ');
				html.push('	</th>');
				html.push('	<td class="width60">');
				html.push('		<select class="input-control property_type"  name="property_type">');
				html.push('			<option value="pro">属性</option>');
				if (k != "initoperate") {
					html.push('			<option value="task">任务</option>');
					html.push('			<option value="workflow">子流程</option>');
				}
				html.push('		</select>');
				html.push('	</td>');
				html.push('	<td >');
				html.push('		<select class="input-control property_name"  name="property_name">');
				html.push('		</select>');
				html.push('	</td>');
				html.push('	<td>');
				html.push('	  <input type="text" class="input-control property_value"  name="property_value"/>');
				html.push('	  <a class="click-config" href="javascript:;" style="font-size:18px;font-weight: bold;display:none;">编辑</a>');
				html.push('	</td>');
				html.push('</tr>');

				var tableHtml = [];
				tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
				tableHtml.push('<thead>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="4" class="pop-panel-header">');
				tableHtml.push($('#' + _div).parent("td").prev("th").text() + "配置");
				tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<tr>');
				tableHtml.push('<th>');
				tableHtml.push('&nbsp;');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('操作类型');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('操作对象');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('操作内容');
				tableHtml.push('</th>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('</thead>');
				tableHtml.push('<tbody></tbody>');
				tableHtml.push('</Table>');
				var $table = $(tableHtml.join('')).appendTo(panel);

				var $add = $('<div class="panel-add-btn ui-widget-header">+</div>').appendTo(panel);
				$add.click(function() {
					that.addRow($table, html, {}, util.getFormType(src));
				});

				var json = myUtil.stringToJson(_props[_k].value);
				for ( var i in json) {
					that.addRow($table, html, json[i], util.getFormType(src));
				}
				$('#' + _div).data('editor', this);
				util.initPopEvent(sle, panel, that);
			};
			this.addRow = function($table, html, data, form) {
				var $tr = $(html.join('')).appendTo($table);
				$tr.find(".panel-row-btn").click(function() {
					$tr.remove();
				});
				var _dv;
				if (data) {
					for ( var k in data) {
						$tr.find("." + k).val(myUtil.jsonArrayToString(data[k]));
					}
					$tr.find(".property_value").data("copy_config", data['property_value']);

					_dv = data["property_name"];
				}

				var $type = $tr.find(".property_type");
				var $field = $tr.find(".property_name");
				var $value = $tr.find(".property_value");
				var $a = $tr.find(".click-config");

				$type.change(function() {
					var val = this.value;
					util.fillSelectOptionNotice($field);
					if (val == "pro") {
						util.loadFormField(form, function(json, value, name) {
							util.fillSelectOption($field, util.returnTreeFormField(json, name, value), _dv, name, value);
						});
						$value.removeAttr("readonly").removeClass("readonly").show();
						$a.hide();
					} else if (val == "task") {
						util.loadAbleTask(form, function(data, value, name) {
							util.fillSelectOption($field, data, _dv, name, value);
						});
						$value.attr("readonly", "readonly").addClass("readonly").show();
						$a.hide();
					} else if (val == "workflow") {
						util.loadAbleWorkflow(form, function(data, value, name) {
							util.fillSelectOption($field, data, _dv, name, value);
						});
						$value.attr("readonly", "readonly").addClass("readonly").hide();
						$a.show();
					}
				}).change();

				$field.change(function() {
					$value.val('');
					$value.removeData('copy_config');
				});

				// 添加 复制字段配置事件

				var editor = this;
				$a.click(function() {
					if ($type.val() == "workflow") {
						var parent = $table.parent().parent();
						copyPanel = $('<div class="pop-editor-panel"/>').appendTo(parent);
						var attr = {
							"z-index" : parseInt(parent.css("z-index")) + 1,
							"height" : "auto"
						}
						copyPanel.text("加载中...").css(attr).show();

						var tableHtml = [];
						tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
						tableHtml.push('<thead>');
						tableHtml.push('<tr>');
						tableHtml.push('<th colspan="2" class="pop-panel-header">');
						tableHtml.push("子流程属性拷贝配置");
						tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
						tableHtml.push('</th>');
						tableHtml.push('</tr>');
						tableHtml.push('<tr>');
						tableHtml.push('<th>');
						tableHtml.push('子流程属性');
						tableHtml.push('</th>');
						tableHtml.push('<th>');
						tableHtml.push('拷贝的值');
						tableHtml.push('</th>');
						tableHtml.push('</tr>');
						tableHtml.push('</thead>');
						tableHtml.push('<tbody></tbody>');
						tableHtml.push('</Table>');

						var copyHtml = [];
						copyHtml.push('<tr>');
						copyHtml.push('<td>');
						copyHtml.push('<input type="hidden" class="copy-control readonly sub_property_name" readonly="readonly" name="sub_property_name" />');
						copyHtml.push('<input type="text" class=" readonly description" readonly="readonly" name="description" />');
						copyHtml.push('</td>');
						copyHtml.push('<td>');
						copyHtml.push('<input type="text" class="copy-control sub_property_value"  name="sub_property_value" />');
						copyHtml.push('</td>');
						copyHtml.push('</tr>');

						// 加载子流程的字段
						var loadWFF = function(json, value, name) {
							copyPanel.empty();
							var $table = $(tableHtml.join('')).appendTo(copyPanel);
							$(".pop-panel-close-btn", copyPanel).click(function() {
								copyPanel.trigger("destroy", [ true ]);
							});
							// 构建配置的值
							var cd = $value.data("copy_config") || {};

							util.buildTreeFormField(json, name, value, function(item) {
								var ctr = $(copyHtml.join(''));
								var key = item["property_name"];
								ctr.find(".sub_property_name").val(key);
								ctr.find(".sub_property_value").val(cd[key]);
								ctr.find(".description").val(item["description"]);
								$table.append(ctr);
							});
						}
						// 加载子流程
						var loadWF = function(data, value, name) {
							if (data && data.length) {
								var l = data.length;
								var _swf = $field.val();
								for (var i = 0; i < l; i++) {
									if (data[i][value] == _swf) {
										util.loadFormField(data[i]["projtype"], loadWFF);
									}
								}
							}
						}
						util.loadAbleWorkflow(form, loadWF);
					}
					var doCopyPanelHide = function(e, h) {
						// var data = [];
						var item = {};
						copyPanel.find("tbody tr").each(function() {
							var val = $(this).find(".sub_property_value").val();
							if (val) {
								var key = $(this).find(".sub_property_name").val();
								item[key] = val;
								// data.push(item);
							}
						});
						$value.val(myUtil.jsonArrayToString(item));
						$value.data("copy_config", item);
						if (h) {
							copyPanel.remove();
							copyPanel = null;
							$table.parent().show();
						}
						// editor.destroy();
					};
					copyPanel.bind("destroy", doCopyPanelHide);

				});
			};
			this.destroy = function() {
				var array = [];
				if (copyPanel) {
					copyPanel.trigger("destroy", [ false ]);
				}
				panel.find("tbody tr").each(function() {
					var item = {};
					$(this).find(".input-control").each(function() {
						var name = $(this).attr("name");
						if (name == "property_value") {
							if (item["property_type"] == "workflow") {
								item[name] = $(this).data("copy_config")
							} else {
								item[name] = $(this).val();
							}
						} else {
							item[name] = $(this).val();
						}
					});
					if (item.property_type == "workflow") {
						item.property_value = myUtil.stringToJson(item.property_value);
					}
					array.push(item);
				});
				_props[_k].value = array;
			};
		},
		knowledgeEditor : function() {
			var _props, _k, _div, _src, _r;
			var panel;
			this.init = function(props, k, div, src, r) {
				_props = props;
				_k = k;
				_div = div;
				_src = src;
				_r = r;

				var that = this;
				var sle = $('<div class="pop-editor-label" style="width:100%;height:100%;"/>').text("...").attr("title", "点击编辑").appendTo('#' + _div);
				panel = $('<div class="pop-editor-panel"/>').appendTo('#' + _div);

				var tableHtml = [];
				tableHtml.push('<Table cellpadding="0" cellspacing="0" width="100%">');
				tableHtml.push('<thead>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="2" class="pop-panel-header">');
				tableHtml.push("知识点配置");
				tableHtml.push('<img src="' + myflow.config.basePath + '/img/close.png" title="关闭" class="pop-panel-close-btn"/>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('</thead>');
				tableHtml.push('<tbody>');
				tableHtml.push('<tr>');
				tableHtml.push('<th width="80">');
				tableHtml.push('一级类别：');
				tableHtml.push('</th>');
				tableHtml.push('<td>');
				tableHtml.push('<select class="input-control first_category"  name="first_category"></select>');
				tableHtml.push('</td>');
				tableHtml.push('<tr>');
				tableHtml.push('<th>');
				tableHtml.push('二级类别：');
				tableHtml.push('</th>');
				tableHtml.push('<th>');
				tableHtml.push('<select class="input-control second_category"  name="second_category"></select>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<tr>');
				tableHtml.push('<th colspan="2">');
				tableHtml.push('词条');
				tableHtml.push('</th>');
				tableHtml.push('</th>');
				tableHtml.push('</tr>');
				tableHtml.push('<td id="keyword-panel"  colspan="2" style="max-height:400px;overflow:auto;">');
				tableHtml.push('');
				tableHtml.push('</td>');
				tableHtml.push('</tr>');
				tableHtml.push('</tbody>');
				tableHtml.push('</Table>');

				var $table = $(tableHtml.join('')).appendTo(panel);

				var data = myUtil.stringToJson(_props[_k].value);

				var value = _props[_k].value || {};
				if (!value.keyword) {
					value.keyword = {};
				}

				var first_category = $(".first_category", $table);
				var second_category = $(".second_category", $table);
				var keyword = $("#keyword-panel", $table);

				first_category.change(function() {
					var fsv = first_category.val();
					if (!fsv) {
						return;
					}
					myUtil.loadDataSources(myflow.config.contextPath + "/frame/knowledge/getCategoryChildrenName.do?_id=" + fsv, function(data) {
						util.fillSelectOption(second_category, data, value["second_category"], "name", "_id", true);
						if (_props[_k].value) {
							_props[_k].value = {};
						}
						_props[_k].value["second_category"] = second_category.val();
					}, "second_category" + fsv);
				});
				myUtil.loadDataSources(myflow.config.contextPath + "/frame/knowledge/knowledge_categoryNameByJson.do", function(data) {
					util.fillSelectOption(first_category, data, value["first_category"], "name", "_id", true);
					if (_props[_k].value) {
						_props[_k].value = {};
					}
					_props[_k].value["first_category"] = first_category.val();
					first_category.change();
				}, "first_category");

				var html = [];
				html.push('<label class="keyword-item inline-block" style="margin:4px 6px;" >');
				html.push('<input type="checkbox" class="selected" style="width:auto;height:auto;min-height:13px;"/>');
				html.push('<input type="hidden" class=" keyword_id" name="keyword_id" />');
				html.push('<input type="hidden" class=" keyword_name" name="keyword_name" />');
				html.push('<span class="text"></span>');
				html.push('</label>');
				keyword.empty();
				myUtil.loadDataSources(myflow.config.contextPath + "/frame/knowledge/knowledge_getAllEntry.do", function(data) {
					if (data && data.length) {
						for ( var i in data) {
							var item = data[i];
							var $item = $(html.join('')).appendTo(keyword);
							var checkbox = $item.find(".selected");
							if (value.keyword[item["_id"]]) {
								checkbox.attr("checked", "checked");
							}
							$item.find(".keyword_id").val(item["_id"]);
							$item.find(".keyword_name").val(item["name"]);
							$item.find(".text").text(item["name"]);
						}
					}
				}, "knowledge_getAllEntry");
				$('#' + _div).data('editor', this);
				util.initPopEvent(sle, panel, that);
			};
			this.destroy = function() {
				var item = {};
				panel.find("tbody tr").each(function() {
					$(this).find(".input-control").each(function() {
						item[$(this).attr("name")] = $(this).val();
					});
				});
				item.keyword = {};
				panel.find(".selected:checked").each(function() {
					var id = $(this).parent().find(".keyword_id").val();
					var name = $(this).parent().find(".keyword_name").val();
					item.keyword[id] = name;
				});
				_props[_k].value = item;
			};
		}
	});

})(jQuery);